
package com.chongchong.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.chongchong.core.boot.ctrl.SysController;
import com.chongchong.core.mp.support.Condition;
import com.chongchong.core.mp.support.Query;
import com.chongchong.core.tool.api.R;
import com.chongchong.core.tool.utils.Func;
import com.chongchong.system.service.IAttachService;
import com.chongchong.system.vo.FileInfoVO;
import com.chongchong.user.entity.Attach;
import com.chongchong.user.vo.AttachVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;

/**
 * 附件表 控制器
 *
 * @author
 */
@RestController
@AllArgsConstructor
@RequestMapping("shanhe-resource/attach")
@Api(value = "附件相关操作", tags = "附件相关操作")
public class AttachController extends SysController {

    private final IAttachService attachService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入attach")
    public R<Attach> detail(Attach attach) {
        Attach detail = attachService.getOne(Condition.getQueryWrapper(attach));
        return R.data(detail);
    }

    /**
     * 分页 附件表
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入attach")
    public R<IPage<Attach>> list(Attach attach, Query query) {
        IPage<Attach> pages = attachService.page(Condition.getPage(query), Condition.getQueryWrapper(attach));
        return R.data(pages);
    }

    /**
     * 自定义分页 附件表
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "自定义分页分页", notes = "传入attach")
    public R<IPage<AttachVO>> page(AttachVO attach, Query query) {
        IPage<AttachVO> pages = attachService.selectAttachPage(Condition.getPage(query), attach);
        return R.data(pages);
    }

    /**
     * 新增 附件表
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入attach")
    public R save(@Valid @RequestBody Attach attach) {
        return R.status(attachService.save(attach));
    }

    /**
     * 修改 附件表
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入attach")
    public R update(@Valid @RequestBody Attach attach) {
        return R.status(attachService.updateById(attach));
    }

    /**
     * 新增或修改 附件表
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入attach")
    public R submit(@Valid @RequestBody Attach attach) {
        return R.status(attachService.saveOrUpdate(attach));
    }


    /**
     * 删除 附件表
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(attachService.removeByIds(Func.toLongList(ids)));
    }

    /**
     * 删除 附件表 + 文件
     */
    @PostMapping("/removeFile")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "逻辑删除附件表 + 文件", notes = "传入ids")
    public R removeFile(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(attachService.deleteFiles(Func.toLongList(ids)));
    }

    @PostMapping("/upload")
    @ApiOperationSupport(order = 9)
    @ApiOperation("上传文件")
    public R<FileInfoVO> upload(@RequestParam("file") @ApiParam("文件") @NotNull(message = "请选择上传文件") MultipartFile file) throws IOException {
        Attach attach = attachService.upload(file);
        FileInfoVO fileInfoVo = new FileInfoVO();
        if (attach != null) {
            fileInfoVo.setId(attach.getId());
            fileInfoVo.setFilename(attach.getOriginalName());
            fileInfoVo.setUrl(attach.getDomain() + attach.getLink());
        }
        return R.data(fileInfoVo);
    }


}
